#ifndef CHESSBOARD_H
#define CHESSBOARD_H

#include <QWidget>
#include <QPainter>
#include <QFont>
#include <QPen>
#include <QColor>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QRect>
#include <QPoint>
#include <QStack>
#include <QMessageBox>

/* 棋子枚举 */
enum Chess
{
    EMPTY = 0,
    RedSoldier,
    RedCannon,
    RedCart,
    RedHorse,
    RedPhase,
    RedChevalier,
    RedGenerals,
    BlackSoldier,
    BlackCannon,
    BlackCart,
    BlackHorse,
    BlackPhase,
    BlackChevalier,
    BlackGenerals
};

/* 步骤类 */
class Steps
{
public:
    void setOldPoint(QPoint &point) { m_oldPoint = point; }

    void setNewPoint(QPoint &point) { m_newPoint = point; }

    void setOldChess(int oldChess) { m_oldChess = oldChess; }

    void setNewChess(int newChess) { m_newChess = newChess; }

    QPoint getOldPoint() { return m_oldPoint; }

    QPoint getNewPoint() { return m_newPoint; }

    int getOldChess() { return m_oldChess; }

    int getNewChess() { return m_newChess; }

private:
    QPoint m_oldPoint;    //原坐标
    QPoint m_newPoint;    //新坐标
    int m_oldChess;       //原棋子
    int m_newChess;       //新棋子
};

class chessboard : public QWidget
{
    Q_OBJECT
public:
    explicit chessboard(QWidget *parent = nullptr);

public:
    /* 获取棋子类型 */
    Chess getPointVal(int row, int col);

    /* 设置棋子类型 */
    void setPointVal(int row, int col, Chess chess);

    void setValid();    //改变点击的有效性

    bool getValid();    //点击是否有效

    bool isRed(int row, int col);   //判断是否是红方棋子

    bool isBlack(int row, int col);   //判断是否是黑方棋子

    void initialize();  //游戏初始化

    int getRow();   //获取行

    int getCol();   //获取列

    void insertSteps(QPoint &oldPoint, QPoint &newPoint, int &oldChess, int &newChess);

    void removeSteps();

    bool isRed();

signals:
    void cellClicked(int oldRow, int oldCol, int row, int col);

    void whereShow();

public slots:

protected:
    void paintEvent(QPaintEvent * event);

    void mousePressEvent(QMouseEvent * event);

private:
    /* 定义棋盘格子的大小 */
    static const int m_row = 10;
    static const int m_col = 9;
    static const int m_cellSize = 100;
    static const int m_margin = m_cellSize / 2;

    /* 棋盘点位数组 */
    int m_point[m_col][m_row];

    bool m_isRed;   //是否是红方回合

    bool m_isChose; //是否选中

    int m_cnt;    //应该是第几次点击

    int m_oldRow;   //原来的row

    int m_oldCol;   //原来的col

    bool m_isValid; //第二次点击是否有效

    bool m_isgameOver;    //游戏是否结束

    QColor m_highlightColor;  //高亮选中棋子

    QString conversion(Chess chess);   //根据值得到对应的棋子

    QStack<Steps> steps;    //步骤栈
};

#endif // CHESSBOARD_H
